/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.lmx.repoDomain.repository;

import java.util.Map;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.lmx.common.util.CommonUtil;
import com.lmx.persistence.service.DeviceTypeService;
import com.lmx.repoDomain.tmodel.DeviceType;

import io.katharsis.queryParams.QueryParams;
import io.katharsis.repository.annotations.JsonApiFindAll;
import io.katharsis.repository.annotations.JsonApiResourceRepository;

@JsonApiResourceRepository(DeviceType.class)
@Component
public class DeviceTypeRepository extends BaseRepository<DeviceType>{

    @PostConstruct
    public void init() {
        dslJpaService.setEntityClass(DeviceType.class); 
    }
    @Autowired
    DeviceTypeService srv;
    @Override
	@JsonApiFindAll
	public 	Iterable<DeviceType> findAll(final QueryParams requestParams) {
		Map<String, String> keyMap = CommonUtil.getKeyMapByFilter(requestParams);		
			if (keyMap==null||!keyMap.containsKey("tenantId")) {//tenantId通过参数传递时是分配租户设备类型
				return super.findAll(requestParams);
			}else{
				Long tetantId=Long.valueOf(keyMap.get("tenantId"));
				if (keyMap.containsKey("forTenant")&&"yes".equals(keyMap.get("forTenant"))) {
					return this.srv.findDeviceTypeList(tetantId);
				}else {
					return this.srv.findDeviceTypesByTenant(tetantId);
				}
			}
	}
}
